package sheep.view.Component;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import sheep.view.utils.swingx.DropShadowPanel;


public class UserCellRenderer implements TableCellRenderer {

  private final Color backColor1=Color.white, backColor2=new Color(237, 242, 200),
          textColor=new Color(21, 21, 21), selectedBackColor=new Color(150, 150, 242),
          selectedTextColor=new Color(240, 244, 251);
  
  boolean border = true;
  
  public static final DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();
  
  private final JPanel panel;
    
    public UserCellRenderer() {
        panel = new DropShadowPanel();
        panel.setOpaque(false);
        panel.setLayout(new BorderLayout());
    }
    
  public void setBorderHeight (boolean b){
    border = b;
  }  
  public Component getTableCellRendererComponent(JTable table, Object value, 
          boolean isSelected, boolean hasFocus, int row, int column) {
    
    Component renderer = DEFAULT_RENDERER.getTableCellRendererComponent(
            table, value, isSelected, hasFocus, row, column);
    
    ((JLabel) renderer).setOpaque(false);
    Color forecolor, backcolor;
    
    if(isSelected) {
      forecolor = selectedTextColor;
      backcolor = selectedBackColor;
      panel.setPreferredSize(new Dimension(30, 100));
    } 
    else {
      forecolor = textColor;
      if(row % 2 == 0) {
        backcolor = backColor1;
      }
      else {
        backcolor = backColor2;
      }
    }
    
    panel.add(renderer, BorderLayout.CENTER);
    
    JPanel temp = new JPanel (new BorderLayout());
    temp.setForeground(forecolor);
    temp.setBackground(backcolor);
    if (border){
      if (column==0){
        temp.add(new OneSideRoundedPanel(panel, OneSideRoundedPanel.OneSideRoundedPanelMode.WEST), BorderLayout.CENTER);
        temp.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 0));
      }else if (table.getColumnCount()>0)
        if (column== table.getColumnCount()-1){
          temp.add(new OneSideRoundedPanel(panel, OneSideRoundedPanel.OneSideRoundedPanelMode.EAST), BorderLayout.CENTER);
          temp.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 5));
      }else{
        temp.add(new OneSideRoundedPanel(panel, OneSideRoundedPanel.OneSideRoundedPanelMode.CENTER), BorderLayout.CENTER);
        temp.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
      }
    }else{
      if (column==0){
        temp.add(new OneSideRoundedPanel(panel, OneSideRoundedPanel.OneSideRoundedPanelMode.WEST), BorderLayout.CENTER);
        temp.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 0));
      }else if (table.getColumnCount()>0)
        if (column== table.getColumnCount()-1){
          temp.add(new OneSideRoundedPanel(panel, OneSideRoundedPanel.OneSideRoundedPanelMode.EAST), BorderLayout.CENTER);
          temp.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 5));
      }else{
        temp.add(new OneSideRoundedPanel(panel, OneSideRoundedPanel.OneSideRoundedPanelMode.CENTER), BorderLayout.CENTER);
        temp.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 0));      
      }
    }
    return temp;
    
  }
  
}